/*
============================================================================
FileName	:	XQChatConfiguration.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-08 13:07
Change Data	:	2012-03-08 13:07
Detail		:	The configuration(struct) of xqchat application
============================================================================
*/

#include <QFile>
#include <QtXml>
#include <QtGui>
#include "XQChatConfiguration.h"
using namespace XQChat;

XQChat::CXQChatConfiguration::CXQChatConfiguration( const QString& jid,const QString& password )
	: m_typeOfClient(XQChatRenren)
{
	setJid(jid);
	setPassword(password);
}

XQChat::CXQChatConfiguration::CXQChatConfiguration( const CXQChatConfiguration& config )
	: QXmppConfiguration(config)
{
	m_qsIcon = config.getIconPath();
	m_qsDefaultHeader = config.getDefaultHeader();
	m_typeOfClient = config.getClientType();
}

XQChat::CXQChatConfiguration::CXQChatConfiguration()
{

}

XQChat::CXQChatConfiguration::~CXQChatConfiguration(void)
{
}

void XQChat::CXQChatConfiguration::setJid( const QString &jid )
{
	QXmppConfiguration::setJid(jid);
	QString qsDomain = domain();		//according to domain to decide use which client.

	//traversal the config map
	QMap<CXQChatConfiguration::XQChatClientType,CXQChatConfiguration::XQChatClientConfig>::iterator
		iter = m_mapClientConfig.begin();
	while(iter!=m_mapClientConfig.end())
	{
        foreach(const QString& qsTmp, iter.value().listDomains)
		{
			if(qsTmp==qsDomain)
			{
				//use this client config and reset the configuration
				resetClientConfig(iter.value());
				return;
			}
		}

		++iter;
	}
}

QString XQChat::CXQChatConfiguration::getClientTypeStr() const
{
	return m_mapClientConfig[m_typeOfClient].qsName;
}

void XQChat::CXQChatConfiguration::resetClientConfig( const CXQChatConfiguration::XQChatClientConfig& config )
{
	setDomain(config.qsDomain);
	setPort(config.iPort);

	m_typeOfClient = config.typeOfClient;
	m_qsIcon = qApp->applicationDirPath() + config.qsIcon;
	m_qsDefaultHeader = qApp->applicationDirPath() + config.qsDefaultHeader;
}

bool XQChat::CXQChatConfiguration::loadClientConfig( QString qsFileName /*"./data/config/ClientConfig.xml"*/ )
{
	QFile file(qsFileName);
	if(!file.open(QIODevice::ReadOnly))
		return false;

	QDomDocument doc;
	if(!doc.setContent(&file))
		return false;

	QDomElement eleRoot = doc.firstChildElement("root");
	if(eleRoot.isNull())
		return false;

	QDomElement eleConfig = eleRoot.firstChildElement("client");
	
	while(!eleConfig.isNull())
	{
		XQChatClientType iType = static_cast<XQChatClientType>(eleConfig.firstChildElement("typevalue").text().toInt());

		m_mapClientConfig[iType].typeOfClient=iType;
		m_mapClientConfig[iType].qsName=eleConfig.firstChildElement("name").text();
		m_mapClientConfig[iType].qsDomain=eleConfig.firstChildElement("domain").text();
		m_mapClientConfig[iType].qsIcon=eleConfig.firstChildElement("icon").text();
		m_mapClientConfig[iType].listDomains.clear();
		m_mapClientConfig[iType].listDomains = eleConfig.firstChildElement("domain_contains").text().split(",");
		m_mapClientConfig[iType].qsDefaultHeader = eleConfig.firstChildElement("default_header").text();

		eleConfig = eleConfig.nextSiblingElement("client");
	}

	return true;
}

QMap<CXQChatConfiguration::XQChatClientType,CXQChatConfiguration::XQChatClientConfig> XQChat::CXQChatConfiguration::m_mapClientConfig;
